﻿using StripeDemo.Utils;
using StripeNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace StripeDemo.Models
{
    public class CardsController : Controller
    {
        //
        // GET: /Card/
        public ActionResult Index()
        {
            ViewBag.Msg = "Create Payment method";
            return View();
        }

        //
        // GET: /Card/
        public ActionResult Create()
        {
            if (HttpContext.Request.RequestType == "POST")
            {
                var myCard = new StripeCardCreateOptions();
                myCard.Source = new StripeSourceOptions()
                {
                    Number = Request.Form[Constant.CARD_NUMBER].ToString(),
                    ExpirationYear = Request.Form[Constant.EXPRIRE_YEAR].ToString(),
                    ExpirationMonth = Request.Form[Constant.EXPRIRE_MONTH].ToString(),
                    Cvc = Request.Form[Constant.CVC].ToString().ToString(),
                    Object = Constant.CARD
                };
                // Create card
                var cardService = new StripeCardService();
                try
                {
                    StripeCard stripeCard = cardService.Create(Request.Form[Constant.CUSTOMER_ID].ToString(), myCard);
                }
                catch (StripeException exception)
                {
                    ViewBag.Msg = exception.StripeError.Message;
                    return View("Index");
                }
            }
            return RedirectToAction("ListAll", "Payment");
        }
    }
}